/**  
 * @Title: Demo.java
 * @Package org.csun.nc.utils.map
 * @Description: TODO
 * @author chisj chisj@foxmail.com
 * @date 2018年9月3日
 */
package com.csun.cmny.provider.utils.map;


import java.awt.geom.Point2D;

/**
 * ClassName: Demo 
 * @Description: TODO
 * @author chisj chisj@foxmail.com
 * @date 2018年9月3日
 */
public class PointUtil {
	
	public static void main(String[] args) {
		Point2D.Double point1 = new Point2D.Double(114.4291917726292,30.418032456341848);
		Point2D.Double point2 = new Point2D.Double(114.42473839917815,30.420710098164225);
		System.out.println(getDistance(point1, point2));
	}
	
	/**
	 * JAVA实现  百度两点间的距离 LH
	 * 	-结果与百度JsApi的getDistance一致
	 * @param point1
	 * @param point2
	 * @return
	 */
	public static double getDistance(Point2D.Double point1,Point2D.Double point2){
		return Wv(point1, point2);
	}
	
	private static double Wv(Point2D.Double a,Point2D.Double b){
		if(a==null || b==null){
			return 0;
		}
		double a_lng = ew(a.x, -180, 180);  
		double a_lat = lw(a.y, -74, 74);  
		double b_lng = ew(b.x, -180, 180);  
		double b_lat = lw(b.y, -74, 74);  
		return Td(oi(a_lng), oi(b_lng), oi(a_lat), oi(b_lat));
	}
	private static double oi(double a){
		 return Math.PI * a / 180;
	}
	private static double Td(double a,double b,double c,double d){
		return 6370996.81 * Math.acos(Math.sin(c) * Math.sin(d) + Math.cos(c) * Math.cos(d) * Math.cos(b - a));
	}
	private static double ew(double a,double b,double c){
		if(a>c){
			 a -= c - b ; 
		}else if(a<b){
			 a += c - b;
		}    
		return a ;
	}
	private static double lw(double a,double b,double c){
		a = max(a,b) ;
		a = min(a,c);
		return a;
	}
	private static double max(double a,double b){
		 if(a>b){
			  return a ;
		 }  
		 return b ;
	}
	private static double min(double a,double c){
		if(a>c){
			 return c ;
		}  
	    return a ;
	}
}
